package main

import (
	"fmt"
	"match-spider/util/prop"
	"time"
	"math/rand"
	"whoscored/util/httputil"
	"encoding/json"
	"strings"
	"whoscored/util/boltdb"
	"strconv"
)

/**
抓取比赛
 */
func fetchMatch(dbKey string,date string)  {

	url:="https://www.whoscored.com/matchesfeed/?d="+date
	//url:="https://www.whoscored.com"
	fmt.Println(url)
	headers:=map[string]string{
		"Pragma":"no-cache",
		//"Accept-Encoding": "gzip, deflate",
		"authority":"www.whoscored.com",
		"method":"GET",
		"path":"/matchesfeed/?d="+date,
		"model-last-mode":"Y4xXZ0xcxvrNgAtNB9FlgfKGAwjaeIggLpyPzocqdgA=",
		"Cookie": "visid_incap_774904=iINxyvH1RSKdJVocn4S/cgCjZVoAAAAAQUIPAAAAAABSHTN36SBh6uyX/MAX4UcQ; _ga=GA1.2.586383088.1516610314; permutive-id=ab06f937-443e-4432-83d7-ab4e2b556060; __gads=ID=6da13b6a195043d9:T=1516610316:S=ALNI_MZbNFloOsGT8E_4RTHnzKBgU0cuig; vg=821ae0b8-2b61-46af-a8b4-21331b25c154; ip=3063167153; vl=1:-8.00|2:CN|3:SICHUAN|4:|5:SICHUAN/|6:SICHUAN/CHENGDU|7:610015|!0; vd=chinatelecom.com.cn; visid_incap_774908=6hgTWJYURbKvDwiPKdeFZgzDbloAAAAAQUIPAAAAAAAya8xmGAjnhg8S+l3NdgjJ; incap_ses_895_774908=GrT3Yb64yne9Tc7+5K1rDO2he1oAAAAAkgbqan3S0D8PG1rjFI7IZQ==; _gid=GA1.2.750042621.1518052041; _dm_sync=true; OX_plg=pm; incap_ses_199_774904=oxDrHnnW/0MIa362IP7CAsque1oAAAAAQ1YvKJoG6Hjd1QYD64H28Q==; _psegs=%5B1920%2C1930%2C2126%2C2441%2C4848%2C3376%2C3377%2C1956%2C1907%2C1932%2C4139%2C2300%5D; fq=1; vq=4425,4425,4425; _gat=1; _gat_subdomainTracker=1; _gat_streamamp=1; permutive-session=%7B%22session_id%22%3A%22b1edce51-5f31-4849-b3b1-6f0885bf52ef%22%2C%22last_updated%22%3A%222018-02-08T02%3A11%3A18.970Z%22%7D",
		"Connection": "keep-alive",
		"Accept": "application/json, text/javascript, */*; q=0.01",
		"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/6.5.11 NetType/WIFI Language/zh_CN",
		"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7",
		"X-Requested-With": "XMLHttpRequest",
		"referer":"https://www.whoscored.com/LiveScores",
	}
	data:=httputil.Request(url,"GET",headers,nil,)
	fmt.Println(data)
	return
	if strings.Contains(data,"ROBOTS"){
		seed:=10*1000+rand.Intn(5000)
		time.Sleep(time.Millisecond*time.Duration(seed))
		fetchMatch(dbKey,date)
	}
	s:=strings.Replace(data,"'","\"",-1)
	s=strings.Replace(s,",,",",\"\",",-1)
	s=strings.Replace(s,",,",",\"\",",-1)
	s=strings.Replace(s,",,",",\"\",",-1)
	s=strings.Replace(s,",,",",\"\",",-1)
	s=strings.Replace(s,",]",",\"\"]",-1)


	slice:=make([]interface{},0)
	json.Unmarshal([]byte(s),&slice)
	//联赛信息
	region:=slice[1].([]interface{})
	regionMap:=make(map[string]interface{})
	for _,v:=range region{
		vv:=v.([]interface{})
		k:=strconv.FormatFloat(vv[0].(float64), 'f', -1, 64)
		regionMap[k]=v
	}

	ss:=slice[2].([]interface{})
	//第15位为1（从0开始）表示有match report
	for _,v:=range ss{

		vv:=v.([]interface{})
		k:=strconv.FormatFloat(vv[0].(float64), 'f', -1, 64)
		bucket:=date
		key:=strconv.FormatFloat(vv[1].(float64), 'f', -1, 64)
		m:=map[string]interface{}{
			"region":regionMap[k],
			"match":vv,
		}
		value,_:=json.Marshal(m)
		boltdb.Put(dbKey,bucket,key,string(value))
	}

}

func main()  {
	startAt:=prop.Get("match","date.start")
	endAt:=prop.Get("match","date.end")
	startTime,_:=time.Parse("2006-01-02",startAt)
	endTime,_:=time.Parse("2006-01-02",endAt)

	for{
		if(endTime.Sub(startTime).Hours()<0){
			break
		}

		nextTimeString:=time.Unix(startTime.Unix(),0).Format("20060102")
		fetchMatch("matchs",nextTimeString)
		seed:=200+rand.Intn(300)
		time.Sleep(time.Millisecond*time.Duration(seed))
		startTime=startTime.Add(time.Hour*24)
	}

}
